home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / dsp / dspgroup / asms.arc / SPECT1K.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-12-09  |  8.4 KB  |  367 lines

  1. #include    <stdio.h>
  2. #include    <dos.h>
  3. #include    <memory.h>
  4. #include    <process.h>
  5. #include        <conio.h>
  6. #include    <graph.h>
  7. #define    CONTROL 0x34
  8. #define    CONT2 0xb4
  9. #define COMLINE 62        /*  (f4)  */
  10. #define THRESH 63     /*   (f5)  */
  11. #define UP 72
  12. #define DOWN 80
  13. #define PGUP 73
  14. #define PGDOWN 81
  15. #define RIGHT 77
  16. #define LEFT 75  
  17. #define CNRIGHT 116
  18. #define CNLEFT 115  
  19. #define HOME 71
  20. #define END 79
  21. #define CNHOME 119
  22. #define CNEND 117
  23. #define CLOSE 61         /* (f3)   */
  24. #define TCONSTU 60       /* (f2)  */ 
  25. #define TCONSTD 59       /* (f1)  */ 
  26. #define TCONST_NEW 68    /* (f10) */
  27. #define GO inp(0x306)
  28. #define STOP inp(0x307)
  29. unsigned int seg320,io320,prt0,cntrlpt,prt1,prt2,zl[512];
  30. int spect_count,y[512],zh[512],threshold;
  31. long z[512];
  32. float norm[512],trate,freqmax;
  33. char *adr1, *adr2;
  34. cdecl main(argc,argv)
  35. int argc;
  36. char *argv[];
  37. {
  38. char c,scr_csts();
  39. int cmin,j,jh,k,i,scale,offset,kount;
  40. int ct,binmax;
  41. long count,jjj,jj,jjmax,jjmore,jjcnt;
  42. float zhold[513],zsum[512],tconst,rate,osc,amplitude;
  43.  
  44. seg320=0xd000;
  45. adr1 = (char *)0xD0001800L;
  46. adr2 = (char *)0xD0001C00L;
  47. tconst=0.5;
  48. jjcnt=0;
  49. io320=0x300;            /*  base i/o address for 320 board */
  50. osc = 25e6/4.;          /*  basic board oscillator */
  51. cntrlpt = io320 + 7;
  52. prt0 = io320 + 4;
  53. prt2 = io320 + 6;
  54. scale = 5;
  55. jjmore = 0;
  56. offset = 180;
  57. clark: do {
  58.     cls();
  59.     printf("Input desired sample rate in Hz ");
  60.     scanf( "%f",&rate);
  61. } while ( (rate < 80.0 ) || (rate > 40000.1));
  62. /* count gives you the oscillator "divisor" just as in SIO's */
  63. count = osc/rate;
  64. cmin = osc /40000.0;
  65. if ( count < 2 ) count = 2;
  66. trate = osc /(float)count;
  67. printf("\nRate = %f ",trate);
  68. /* set timers for desired sampling rate */
  69. control_timer(count,osc,&trate);
  70. _settextposition(24,1);
  71. printf("%7.2f Hz     ",trate);
  72. for(k=0;k<20000;k++) {       /* time delay for 8254 & clear holding arrays */
  73.       
  74.     if (k<513) {
  75.         zhold[k]=0.;
  76.     if (k<512) norm[k]=0.;
  77.     }
  78. }     
  79.  
  80. STOP;   /* halt */
  81. /* Load the fft and square routines */
  82. spawnl(P_WAIT,"intel",argv[0],"ffthy1k","d000","300","0",NULL);
  83. /* Run it once to set up arrays and have proper values in the ports */
  84. GO; /* go */
  85. while(inp(io320)!=88) {};
  86. STOP; /* stop */
  87. /* Generate Reference Baseline Spectrum */
  88.     printf("\n How many samples in reference spectrum? ");
  89.     scanf("%d",&spect_count);
  90. ref_spect(spect_count);
  91. STOP;
  92. printf("\nReference spectrum --- Hit any character when ready");
  93. ci();
  94. _setvideomode(_TEXTC80);
  95. _settextposition(1,1);
  96.     printf("\n How many samples for each displayed spectrum? ");
  97.     scanf("%d",&spect_count);
  98.  
  99. /* Set it up to run forever until ^C or F3 to change screen and quit*/
  100. _setvideomode(_HRESBW);
  101. _setviewport(0,8,639,176);
  102. kount=0; 
  103. GO;
  104. while (1) 
  105. {
  106.     if ((c = scr_csts()) != 0 )
  107.         {
  108.          switch(c) {
  109.              case COMLINE: {
  110.                  _setvideomode(_TEXTC80);;
  111.                  goto clark;
  112.                 break;
  113.             }
  114.             case THRESH: {
  115.                 _setvideomode(_TEXTC80);;
  116.                 printf("Input threshold %d? ",threshold);
  117.                 scanf("%d",&threshold);
  118.                 if (threshold < 1) threshold=0;
  119.                 if (threshold > 199) threshold = 199;
  120.                 if (threshold==0) outp(97,inp(97)&0xFC);
  121.                 _setvideomode(_HRESBW);
  122.                 _setviewport(0,8,639,176);
  123.                 break;
  124.             }
  125.             case UP: {
  126.                 scale -= 1;
  127.                 if (scale < 0) scale = 0;
  128.                 break;
  129.             }
  130.             case DOWN: {
  131.                 scale += 1;
  132.                 if (scale > 15) scale = 15;
  133.                 break;
  134.             }
  135.             case PGUP: {
  136.                 offset -= 10;
  137.                 if (offset < 0) offset = 0;
  138.                 break;
  139.             }
  140.             case PGDOWN: {
  141.                 offset += 10;
  142.                 if (offset > 180) offset = 180;
  143.                 break;
  144.             }
  145.             case CLOSE: {
  146.                 STOP; /* halt */
  147.                 outp(97,inp(97)&0xFC);
  148.                 _setvideomode(_TEXTC80);;
  149.                 exit();
  150.                 break;
  151.             }
  152.             case HOME: {
  153.                 jjmore -= 1;
  154.                 if (jjmore < 0) jjmore = 0;
  155.                 break;
  156.             }
  157.             case END: {
  158.                 jjmore += 1;
  159.                 if (jjmore > 10000) jjmore = 10000;
  160.                 break;
  161.             }
  162.             case CNHOME: {
  163.                 jjmore /= 2;
  164.                 if (jjmore < 0) jjmore = 0;
  165.                 break;
  166.             }
  167.             case CNEND: {
  168.                 jjmore *= 2;
  169.                 if (jjmore > 10000) jjmore = 10000;
  170.                 break;
  171.             }
  172.                  case TCONSTU: {
  173.                 tconst *= 2.0;
  174.                 if (tconst>1.0) tconst=1.0;
  175.                 break;
  176.             }   
  177.             case TCONST_NEW: {
  178.                 _setvideomode(_TEXTC80);; printf("%3d\n",c);
  179.                 printf(" tconst currently = %9.7f --- Enter new value: ",tconst);
  180.                 scanf("%f",&tconst); cls();
  181.                 if (tconst>  1.0) tconst= 1.0;
  182.                 if (tconst<.0001) tconst=.0001 ;
  183.                 _setvideomode(_HRESBW);
  184.                 _setviewport(0,8,639,176);
  185.                 break;
  186.             }
  187.                  case TCONSTD: {
  188.                 tconst *= 0.5;
  189.                 if (tconst<.0001) tconst=.0001 ;     
  190.                 break;
  191.             }   
  192.             default:
  193.                 break;
  194.         }
  195.     }
  196.  
  197. if (z[100]+z[175]+z[200]+z[250]+z[300]+z[400] > (1<<6))  /* is there data? */
  198.   {          /* bypass the following loop if there isn't */
  199. for (k=0;k<512;k++) {
  200.          if (kount == 0) {
  201.             zsum[k]=z[k];
  202.         }
  203.         else zsum[k] += z[k];
  204. }    
  205. kount++;
  206. if (kount >= spect_count)   {   
  207.         kount=0;
  208.     for (k=0;k<512;k++) zsum[k] *= norm[k];        
  209.         amplitude=0.;
  210.         binmax=128;
  211.     for(k=1;k<511;k++)        {
  212.         zhold[k]=(1.0-tconst)*zhold[k] +
  213.           (zsum[k]*2.0+zsum[k-1]+zsum[k+1])*0.25*tconst;
  214.         y[k]= offset-(((int)zhold[k])>>scale);
  215.             zsum[k] = 0;
  216.             if (y[k] < 1) y[k]=1;                  /* don't go off the top */
  217.         if (k > 80 && zhold[k]>amplitude) {    /* avoid DC peak */
  218.             binmax=k;
  219.             amplitude=zhold[k];
  220.         }
  221.     }
  222.         _settextposition(24,1);
  223.         freqmax=((float)binmax)*trate/1024.0;
  224.     printf("Rate= %5.0f Hz Scale= %d tconst= % 5.4f sampl= %d freq=% 5.0f amp=% 4.0f  ",
  225.             trate,scale,tconst,spect_count,freqmax,amplitude);
  226.     sigpts(&y[0],512);      /* show old fft */
  227.     if (threshold!=0){
  228.         if (y[binmax]<threshold) {
  229.             outp(97,inp(97)&0xFC);
  230.             prt1=1193180.0/freqmax;
  231.             outp(67,182);
  232.             outp(66,prt1&0xFF);
  233.             outp(66,(prt1>>8)&0xFF);
  234.             outp(97,inp(97)|3);
  235.     }
  236.     else outp(97,inp(97)&0xFC);
  237.     }
  238.  
  239.    }
  240.   }     /* end of bypass loop -- display counter even if no data at input */
  241.       
  242.         _settextposition(1,1);  printf("% 5d   ",jjcnt++);
  243.         if (jjcnt>32675) jjcnt=0;
  244.  
  245.     while(inp(io320)!=88) {};
  246.     STOP;  /* halt */
  247.     memcpy((char *)zh,adr1,1024);
  248.     memcpy((char *)zl,adr2,1024);
  249.     GO; /* go */
  250.     for(k=0;k<512;k++) {
  251.         z[k]=zh[k];
  252.         z[k]=(z[k]<<16)+zl[k];
  253.     }
  254.   }
  255. }
  256.  
  257. cls()
  258. {
  259.     printf("\x1B[2J");
  260. }
  261.  
  262. sigpts(y,n)
  263. int *y,n;
  264. {
  265.     int center;
  266.     register int k;
  267.     center = (640-n)/2;
  268.     for(k=0;k<n;k++){
  269.         _moveto(k+center,177);
  270.         _setcolor(1);
  271.         _lineto(k+center,*(y+k));
  272.         _setcolor(0);
  273.         _lineto(k+center,0);
  274.     }
  275. }
  276.     
  277. ci()
  278. {
  279.     int j;
  280.     while (kbhit()==0) {};
  281.       j=getch();
  282. }
  283.  
  284. char scr_csts()
  285. {
  286.     char c;
  287.     if (kbhit()==0) return(0);
  288.     if (getch()!=0) return(0);
  289.     return(getch());
  290. }
  291.  
  292. control_timer(cnt,osc,trate)
  293. long cnt;
  294. float *trate,osc;
  295. {
  296.     int ctl,cth;
  297.     /* tell board timer info coming */
  298.     outp(cntrlpt ,CONTROL);
  299.     /* count is timing divisor */
  300.     ctl = cnt & 0xff;    /* lo byte */
  301.     cth = ( cnt >> 8 ) & 0xff;/* hi byte */
  302.     /* control timer 0 */
  303.     outp(prt0 , ctl);
  304.     outp(prt0 , cth);
  305.     /* control timer 2 */
  306.     outp(cntrlpt , CONT2);
  307.     outp(prt2 , ctl);
  308.     outp(prt2 , cth);
  309.     /* compute new sampling rate  for output on screen*/
  310.     *trate = osc/(float)cnt;
  311. }
  312. ref_spect(spect_count)
  313. int spect_count;
  314. {
  315. float t1,t2,t3,t4,norm_max;
  316. int binmax,jj,k;
  317.  
  318.         GO; /* go */
  319. for(k=0;k<512;k++) norm[k] = 0.0;
  320. for (jj=0;jj<spect_count;jj++) {
  321.         _settextposition(4,1);printf("% 5d  ",jj);
  322.     while(inp(io320)!=88) {};
  323.     STOP;  /* halt */
  324.     memcpy((char *)zh,adr1,1024);
  325.     memcpy((char *)zl,adr2,1024);
  326.     GO; /* go */
  327.     for(k=0;k<512;k++) {
  328.         z[k]=zh[k];
  329.         z[k]=(z[k]<<16)+zl[k];
  330.     }
  331.         for(k=0;k<512;k++) norm[k] += z[k];
  332. }
  333. binmax=256;
  334. norm_max=-999.;
  335. t1 = t2 = t3 = t4 = 0.0;
  336. for (k=0;k<506;k++) {    /* start with 7-point running average smoothing */
  337.     t4=t3;
  338.     t3=t2;
  339.     t2=t1;
  340.     t1=norm[k]+norm[k+1]+norm[k+2]+norm[k+3]+norm[k+4]+norm[k+5]+norm[k+6];
  341.     norm[k]=t4/7.0;          /* keep centered at proper index */
  342.     
  343.        if ( norm[k] > norm_max ) {
  344.            binmax = k;
  345.            norm_max = norm[k] ;
  346.        }    
  347.    }
  348. for (k=0;k<25;k++) {
  349.        norm[k]=0.;
  350.        norm[511-k]=0.;
  351.      }  
  352. for (k=0;k<512;k++) {
  353.     y[k]=180 - (norm[k]/norm_max)*100.;
  354.     if ((norm[k]>100) && (k>60)) 
  355.                  norm[k] = norm_max/(norm[k]*512.0);
  356.     else         norm[k] = 0.;
  357. }
  358.         _setvideomode(_HRESBW);
  359.         _setviewport(0,8,639,176);
  360.         sigpts(&y[0],510);
  361.            _settextposition(24,1);
  362. printf("Rate = %7.2f Hz                          freq=% 7.1f amp=% 8.1f  ",
  363.             trate,         ((float)binmax)*trate/512.0,norm_max );
  364.  
  365. }
  366.  
  367.